package gms.actions.field;

import java.io.IOException;
import java.util.List;

import gms.entities.field.Field;
import gms.entities.field.Field_rental;
import gms.entities.field.Rent;
import gms.entities.um.User;
import gms.service.field.FieldService;

public class FieldAction extends SuperAction{

	private Field field;
	private Field_rental field_rental;
	private FieldService fieldService;
	
	public Field getField() {
		return field;
	}

	public void setField(Field field) {
		this.field = field;
	}

	public Field_rental getField_rental() {
		return field_rental;
	}

	public void setField_rental(Field_rental field_rental) {
		this.field_rental = field_rental;
	}

	/**
	 * spring ע��service
	 */
	public void setFieldService(FieldService fieldService) {
		this.fieldService = fieldService;
	}

	//场地添加 保存操作
	public String save(){
		System.out.println(field.getField_mark());
		fieldService.saveField(field);
		return "success_add";
	}
	//场地搜索
	public String search(){
		String type = field.getField_type();
		try {
			type = new String(type.getBytes("iso-8859-1"),"utf-8");
			
			System.out.println(type);
			
			//这个循环进不去
			if(type.equals("no")){
				User u = (User) session.getAttribute("user");
				List<Field> list = fieldService.findUserField(u.getUserName());
				session.setAttribute("allRecord", list);
			}else{
				List<Field> list = fieldService.searchByType(type);
				session.setAttribute("allRecord", list);
			}
			
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return "success_show";
	}
	
	//场地修改后进行更新
	public String update(){
		fieldService.updateField(field);
		List<Field> list = fieldService.findAllField();
		session.setAttribute("allRecord", list);
		return "success_show";
	}
	//ajax 进行场地的删除
	public void delete(){
		try {
			if(field.getFacilityId() != -1){
				fieldService.deleteField(field);
				response.getWriter().print("1");
			}else{
				response.getWriter().print("0");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//展示所有场地信息，不论有没有被租用
	public String show(){
		User u = (User) session.getAttribute("user");
		List<Field> list = fieldService.findUserField(u.getUserName());
		session.setAttribute("allRecord", list);
		return "success_show";
	}
	//处理订单
	public void order(){
		User u = (User) session.getAttribute("user");
		List<Rent> list = fieldService.findRentByUsername(u.getUserName());
		try {
			String str = " ";
			for(Rent r : list){
				
				 str +="<tr>"
						 +"<td>"+r.getRentId()+"</td>"
						 +"<td>"+r.getFacilityId()+"</td>"
						 +"<td>"+r.getUsername()+"</td>"
						 +"<td>"+r.getRent_time()+"</td>"
						 +"<td>"+r.getPayment_amount()+"</td>"
						 +"</tr>";
			}
			response.getWriter().print(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//租用场地
	public void rent(){
		System.out.println(field.getFacilityId());
		User u = (User) session.getAttribute("user");
		//ajax响应，前端可以根据响应来设置对应的样式
		try {
			if(field.getFacilityId() != -1){
				String isMark = fieldService.rent(field.getFacilityId(),u.getUserName());
				//判断当前操作时租用还是取消
				if(isMark.equals("true")){
					//租用
					response.getWriter().print("1");
				}else if(isMark.equals("false")){
					//取消
					response.getWriter().print("2");
				}
			}else{
				response.getWriter().print("0");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//修改场地租金的处理逻辑
	public String modifyRental(){
		
		List<Field_rental> rental_list = fieldService.modifyRental();
		//将数据库对象放在request中就可以带过去
		request.setAttribute("rental_list", rental_list);
		return "success_modifyRental";
	}
	//更新场地租金的处理逻辑
	public void updateRental(){
		try {
			fieldService.updateRental(field_rental);
			//更新成功
			response.getWriter().print("1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	

}
